package com.tvein.sanguo.action.controller.tool
{
	//flash
	//pureMVC
	import flash.display.DisplayObject;
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import com.tvein.sanguo.action.common.GameConstants;
	import com.tvein.sanguo.action.view.ActionMediator;
	import com.tvein.sanguo.action.model.SystemProxy;
	import com.tvein.sanguo.swc.action.tool.ToolMission;
	import com.tvein.sanguo.swc.action.tool.ToolMail;
	import com.tvein.sanguo.swc.action.tool.ToolMailNew;
	import com.tvein.sanguo.swc.action.tool.ToolMailInfo;
	import com.tvein.sanguo.swc.action.tool.ToolAdvice;
	import com.tvein.sanguo.swc.action.tool.ToolGold;
	import com.tvein.sanguo.swc.action.tool.ToolBug;
	import com.tvein.sanguo.swc.action.tool.ToolEdition;
	import com.tvein.sanguo.swc.action.tool.ToolWorker;
	import com.tvein.sanguo.swc.action.tool.ToolSound;
	import com.tvein.sanguo.swc.action.tool.ToolExit;
//
	public class ActionTool extends SimpleFabricationCommand implements ICommand
	{
		private var actionMediator:ActionMediator;
		private var systemData:SystemProxy;
		
		override public function execute(notification:INotification) : void
		{
			actionMediator = facade.retrieveMediator(ActionMediator.NAME) as ActionMediator;
			systemData = facade.retrieveProxy(SystemProxy.NAME) as SystemProxy;
			
			clearChild();
			
			var params:Object = new Object();
			
			switch(notification.getBody()[0])
			{
				case "mission":
					var toolMission:ToolMission = new ToolMission();
					actionMediator.action.addChild(toolMission);
					toolMission.name = "ToolMission";
					
					params.Volume = systemData.effectVol;
					toolMission.console("setEffect", params);
					
					routeNotification(GameConstants.GAME_CONSOLE, ["-gameCommand", "GetGameData(Action,GetRoleMission)"], null, "*");
					
					toolMission.addEventListener("Tool-Mission", actionMediator._handleEvents);
					
					break;
				case "mail":
					var toolMail:ToolMail = new ToolMail();
					actionMediator.action.addChild(toolMail);
					toolMail.name = "ToolMail";
					
					params.Volume = systemData.effectVol;
					toolMail.console("setEffect", params);
					
					routeNotification(GameConstants.GAME_CONSOLE, ["-gameCommand", "GetGameData(Action,GetRoleMail)"], null, "*");
					
					toolMail.addEventListener("Tool-Mail", actionMediator._handleEvents);
					
					break;
				case "mailNew":
					var toolMailNew:ToolMailNew = new ToolMailNew();
					actionMediator.action.addChild(toolMailNew);
					toolMailNew.name = "ToolMailNew";
					
					params.Volume = systemData.effectVol;
					toolMailNew.console("setEffect", params);
					
					if (notification.getBody()[1])
					{
						toolMailNew.console("setData", notification.getBody());
					}
					
					toolMailNew.addEventListener("Tool-MailNew", actionMediator._handleEvents);
					
					break;
				case "mailInfo":
					var toolMailInfo:ToolMailInfo = new ToolMailInfo();
					actionMediator.action.addChild(toolMailInfo);
					toolMailInfo.name = "ToolMailInfo";
					
					params.Volume = systemData.effectVol;
					toolMailInfo.console("setEffect", params);
					
					routeNotification(GameConstants.GAME_CONSOLE, ["-gameCommand", "GetGameData(Action,GetRoleMailInfo," + notification.getBody()[1] + ")"], null, "*");
					
					toolMailInfo.addEventListener("Tool-MailInfo", actionMediator._handleEvents);
					
					break;
				case "advice":
					var toolAdvice:ToolAdvice = new ToolAdvice();
					actionMediator.action.addChild(toolAdvice);
					toolAdvice.name = "ToolAdvice";
					
					params.Volume = systemData.effectVol;
					toolAdvice.console("setEffect", params);
					
					toolAdvice.console("setData", notification.getBody()[1]);
					
					toolAdvice.addEventListener("Tool-Advice", actionMediator._handleEvents);
					
					break;
				case "gold":
					var toolGold:ToolGold = new ToolGold();
					actionMediator.action.addChild(toolGold);
					toolGold.name = "ToolGold";
					
					params.Volume = systemData.effectVol;
					toolGold.console("setEffect", params);
					
					routeNotification(GameConstants.GAME_CONSOLE, ["-gameCommand", "GetGameData(Action,GetUserPoint)"], null, "*");
					
					toolGold.addEventListener("Tool-Gold", actionMediator._handleEvents);
					
					break;
				case "bug":
					var toolBug:ToolBug = new ToolBug();
					actionMediator.action.addChild(toolBug);
					toolBug.name = "ToolBug";
					
					params.Volume = systemData.effectVol;
					toolBug.console("setEffect", params);
					
					toolBug.addEventListener("Tool-Bug", actionMediator._handleEvents);
					
					break;
				case "worker":
					var toolWorker:ToolWorker = new ToolWorker();
					actionMediator.action.addChild(toolWorker);
					toolWorker.name = "ToolWorker";
					
					params.Volume = systemData.effectVol;
					toolWorker.console("setEffect", params);
					
					routeNotification(GameConstants.GAME_CONSOLE, ["-gameCommand", "GetGameData(Action,GetSystemInfo,WorkGroup)"], null, "*");
					
					toolWorker.addEventListener("Tool-Worker", actionMediator._handleEvents);
					
					break;
				case "edition":
					var toolEdition:ToolEdition = new ToolEdition();
					actionMediator.action.addChild(toolEdition);
					toolEdition.name = "ToolEdition";
					
					params.Volume = systemData.effectVol;
					toolEdition.console("setEffect", params);
					
					routeNotification(GameConstants.GAME_CONSOLE, ["-gameCommand", "GetGameData(Action,GetSystemInfo,Edition)"], null, "*");
					
					toolEdition.addEventListener("Tool-Edition", actionMediator._handleEvents);
					
					break;
				case "sound":
					var toolSound:ToolSound = new ToolSound();
					actionMediator.action.addChild(toolSound);
					toolSound.name = "ToolSound";
					
					params.Volume = systemData.musicVol;
					toolSound.console("setMusic", params);
					
					params.Volume = systemData.effectVol;
					toolSound.console("setEffect", params);
					
					params.Volume = systemData.voiceVol;
					toolSound.console("setVoice", params);
					
					toolSound.addEventListener("Tool-Sound", actionMediator._handleEvents);
					
					break;
				case "exit":
					var toolExit:ToolExit = new ToolExit();
					actionMediator.action.addChild(toolExit);
					toolExit.name = "ToolExit";
					
					params.Volume = systemData.effectVol;
					toolExit.console("setEffect", params);
					
					toolExit.addEventListener("Tool-Exit", actionMediator._handleEvents);
					
					break;
			}
		}
		
		private function clearChild():void
		{
			for (var i:int = actionMediator.action.numChildren - 1; i >= 0; i--)
			{
				actionMediator.action.removeChildAt(0); 
			}
		}
	}
}